package com.prj.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.prj.entity.TM;
import com.prj.service.TMService;


@RestController
public class QuestionController {

	@Autowired
	TMService tmService;

	@RequestMapping("/getAsk")
	public Page getQuestionData(TM tm) {
		Page page = tmService.queryList(tm);
		return page;
	}
	
	@RequestMapping("/update")
	public TM update(TM tm) {
		TM entity = tmService.getById(tm.getTkId());
		if(tm.getErrorFlag() == 1) {
			entity.setErrorFlag(1);
			entity.setErrorCount(entity.getErrorCount()+1);
			entity.setUpdateTime(new Date());
		}
		entity.setFavFlag(tm.getFavFlag());
		tmService.updateById(entity);
		return tm;
	}
	
	@RequestMapping("/removeErr")
	public TM removeErr(TM tm) {
		TM entity = tmService.getById(tm.getTkId());
		entity.setErrorFlag(0);
		entity.setErrorCount(0);
		tmService.updateById(entity);
		return tm;
	}
	
	@RequestMapping("/updateRand")
	public TM updateRand(TM tm) {
		tmService.updateRand(tm.getType());
		return tm;
	}
	
	@RequestMapping("/updateSuccess")
	public TM updateSuccess(TM tm) {
		tmService.updateSuccess(tm.getTkId());
		return tm;
	}

	@RequestMapping("/getType")
	public List<TM> getType(TM tm) {
		List<TM> tmList = tmService.list(new QueryWrapper<TM>().select("distinct dl_type, tk_dl")
															   .eq(tm.getFavFlag()==1, "fav_flag",1)
															   .eq(tm.getErrorFlag()==1, "error_flag",1)
															    .last(" order by 1  ")
//												  				.last(StrUtil.isBlank(tm.getRand()), " order by tk_id ")
												  				);
		return tmList;
	}

}
